/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package openfireconnection.group.model;

import java.util.ArrayList;

/**
 *
 * @author Miguel y Juanan - Telecor
 */
public class GroupModel {
    private String name;
    private String description;
    private ArrayList<MemberModel> members;
    private ArrayList<PropertyModel> propertys;
    
    public GroupModel(String name) {
        this.name = name;
        
        members = new ArrayList<MemberModel>();
        propertys = new ArrayList<PropertyModel>();
        
        //Agregamos las propiedades por defecto
        PropertyModel showInRoster = new PropertyModel();
        showInRoster.setNameProperty("sharedRoster.showInRoster");
        showInRoster.setValueProperty("nobody");
        
        PropertyModel displayName = new PropertyModel();
        displayName.setNameProperty("sharedRoster.displayName");
        displayName.setValueProperty("");
        
        PropertyModel groupList = new PropertyModel();
        groupList.setNameProperty("sharedRoster.groupList");
        groupList.setValueProperty("");
        
        propertys.add(showInRoster);
        propertys.add(displayName);
        propertys.add(groupList);
    }
    
    public GroupModel() {
        members = new ArrayList<MemberModel>();
        propertys = new ArrayList<PropertyModel>();
    }
    
    
    public ArrayList<PropertyModel> getPropertys() {
        return propertys;
    }
    
    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public ArrayList<MemberModel> getMembers() {
        return members;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public void setMembers(ArrayList<MemberModel> members) {
        this.members = members;
    }

    public void setPropertys(ArrayList<PropertyModel> propertys) {
        for(int i=0; i<propertys.size(); i++) {
            boolean encontrado = false;
            for(int j=0; j<this.propertys.size(); j++) {
                //Si es la misma propiedad
                if(this.propertys.get(j).getNameProperty().equals(
                        propertys.get(i).getNameProperty())) {
                    this.propertys.set(j, propertys.get(i));
                    encontrado = true;
                }
            }
            
            if(!encontrado) {
                this.propertys.add(propertys.get(i));
            }
        }
    }
    
    @Override
    public boolean equals(Object groupModel) {
        if(groupModel == null) {
            return false;
        } else {
            if(groupModel instanceof GroupModel)
            {
                //Si la descripción y el nombre son iguales
                if(((GroupModel)groupModel).getDescription().equals(description) &&
                        ((GroupModel)groupModel).getName().equals(name)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
    
    @Override
    public int hashCode() {
        int result = 17;
        result *= 31 * name.hashCode();
        result *= 31 * description.hashCode();
        return result;
    }
}
